import CustomNavBar from '@/components/CustomNavBar/CustomNavBar.vue';
import EmptyRecord from '@/components/EmptyRecord/EmptyRecord.vue';
import PromptPopup from '@/components/PromptPopup/PromptPopup.vue';
import popupChannelVue from '@/components/popupChannel/popupChannel.vue';
import api from '../../../http/api';
export default {
	components: {
		CustomNavBar,
		EmptyRecord,
		PromptPopup,
		popupChannelVue
	},
	data() {
		return {
			dataList: [],
			page: 1, // 当前页码
			pageSize: 10, // 每页数据量
			isLoading: true, // 防止重复加载
			noMoreData: false, // 是否s已无更多数据

			pointOut: "", //提示窗提示信息
			collectionChannelList: [], //收款渠道
			
			bank_id:'',//选中收款方式的id
			accessStatus:'',//接单状态
			
			popupType:'',////弹窗的类型、用途标识
			
		}
	},
	onLoad() {
		this.loadData(); // 首次加载数据
	},
	onShow(){
		this.loadData(); // 首次加载数据
	},
	onReachBottom(){
		this.loadData();
	},
	methods: {
		// 初始化加载数据
		loadData() {
			api.getCollectionAccountNumber({
			  page: this.page,
			  limit: this.pageSize
			}).then(res=>{
				if (res.data.list.length === 0) {
				  this.noMoreData = true; // 标记无更多数据
				  uni.showToast({ title: '没有更多数据了', icon: 'none' });
				} else {
					if(res.data.page == 1){
						this.dataList = res.data.list;
						this.noMoreData = false;
						this.page++;
					}else{
						this.dataList = [...this.dataList, ...res.data.list]; // 合并新数据
						this.page++;
					}
				  
				}
			}).catch(err=>{
				console.error('加载数据失败:', err);
			}).finally(fee=>{
				this.isLoading = false;
			})
		},
		//获取收款渠道
		CollectionChannel() {
			api.getCollectionChannel().then(res => {
				if (res.code == 1) {
					this.collectionChannelList = res.data
					this.collectionChannelList.forEach(item => {
						item.checked = false;
					})
				}else{
					uni.showToast({ title: res.mag, icon: 'none' });
				}
			}).catch(err => {
				console.error('加载数据失败:', err);
			})
		},
		// 修改后的modifyList处理函数
		modifyList(updatedList) {
			this.collectionChannelList = updatedList;
		},
		//编辑账号
		edit(item) {
			uni.setStorage({
				key:'edit',
				data:item
			})
			uni.navigateTo({
				url:'/pages/Home/edit/index'
			})
		},
		//删除
		clear(id) {
			this.pointOut = "您确定要删除该收款账号吗？";
			this.bank_id = id;
			this.popupType = 'deletePaymentMethod';
			this.$refs.pointOut.open();
		},
		//一键接单
		getOrder(type,id) {
			this.bank_id = id;
			if(type == 0){
				this.popupType = 'addPaymentMethod';
				this.pointOut = "您确定该账号开始接单吗？"
				this.$refs.pointOut.open();
				this.accessStatus = 1
			}else if(type == 1){
				this.popupType = 'addPaymentMethod';
				this.pointOut = "您确定该账号停止接单吗？"
				this.$refs.pointOut.open();
				this.accessStatus = 0
			}
		},
		//提示弹窗点击确认
		collectMoney(data) {
			this.$refs.pointOut.closeModal();
			if(data.popupType=='addPaymentMethod'){
				api.accessOrder({
					'bank_id':this.bank_id,
					'status':this.accessStatus
				}).then(res=>{
					if(res.code ==1){
						this.page = 1;
						this.loadData();
					}else{
						uni.showToast({ title: res.msg, icon: 'error' });
					}
				}).catch(err=>{
					
				})
			}else if(data.popupType=='deletePaymentMethod'){
				api.deletePaymentMethod({
					'bank_id':this.bank_id
				}).then(res=>{
					if(res.code ==1){
						uni.showToast({ title: '删除成功', icon: 'success' });
						this.page = 1;
						this.loadData();
					}else{
						uni.showToast({ title: res.msg, icon: 'error' });
					}
				}).catch(err=>{
					
				})
			}
			
		},
		//收款渠道图片
		payimg(value){
			if(value==1){
				return '/static/payImg/yinghangkazhuangzhang.png'
			}else if(value==2){
				return '/static/payImg/weixin.png'
			}else if(value==3){
				return '/static/payImg/zhifubao.png'
			}else if(value==4){
				return '/static/payImg/yunshanfu.png'
			}else if(value==5){
				return '/static/payImg/shuzirenmingbi.png'
			}else if(value==6){
				return '/static/payImg/dingding.png'
			}
		},
		//提示弹窗关闭
		closeevent(){
			// this.bank_id = ''
		},
		//添加收款账号弹窗
		addmoneyMoney(id) {
			this.bank_id = id,
			this.$refs.collectMoney.open();
			this.CollectionChannel();
		},
		//跳转增加收款方式
		addmoney(data){
			console.log(data)
			this.$refs.collectMoney.closeModal();
			uni.navigateTo({
				url:'/pages/Home/paymentMethod/index?id=' + data.selected.id + "&&name="+data.selected.name
			})
		}
	},
	onPullDownRefresh() {
		this.page = 1
		this.loadData(); // 首次加载数据
		setTimeout(()=>{
			uni.stopPullDownRefresh()//下拉停止
			uni.showToast({ title: '刷新成功', icon: 'success' });
		},1500)
	},
	computed: {
		totalNavHeight() {
			const app = getApp();
			// 确保 globalData 存在，提供默认值
			return (app.globalData?.statusBarHeight || 0) + (app.globalData?.navBarHeight || 44);
		}
	}
}